package com.yuan.base.framework.image;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.request.RequestOptions;
import com.yuan.base.R;
import com.yuan.base.framework.BaseTokenManager;
import com.yuan.base.framework.http.BaseUrl;
import com.yuan.base.utils.LogUtil;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.security.MessageDigest;
import java.util.concurrent.ExecutionException;


/**
 * 图片加载类
 */
public class BaseImageLoad {

	public static int defaultDrawableId = R.drawable.base_ic_loading;

	public static int headDrawableId = R.drawable.base_ic_default_head;


	public static void registerDefault(int defaultDrawableId, int headDrawableId) {
		BaseImageLoad.defaultDrawableId = defaultDrawableId;
		BaseImageLoad.headDrawableId = headDrawableId;
	}

	public class Type {
		public static final int normal = 0;//无修改
		public static final int circular = 1;//圆形
		public static final int radius = 2;//圆角
	}


	public static void loadLocal(String path, ImageView imageView) {
		x.image().bind(imageView, path, new ImageOptions.Builder().setFailureDrawableId(defaultDrawableId)
				.setLoadingDrawableId(defaultDrawableId)
				.setFadeIn(true).setCrop(true).setForceLoadingDrawable(false).build());
	}

	public static void loadCircularAvatar(String id, ImageView imageView) {
		load(id, imageView, Type.circular, headDrawableId);
	}

	public static void loadRadiusAvatar(String id, ImageView imageView) {
		load(id, imageView, Type.radius, headDrawableId);
	}

	public static void load(String id, ImageView imageView) {
		load(id, imageView, Type.normal, defaultDrawableId);
	}

	public static void load(String id, ImageView imageView, int defaultImg) {
		load(id, imageView, Type.normal, defaultImg);
	}

	public static void load(String id, ImageView imageView, int type, int defaultImg) {
		load(id, imageView, type, defaultImg, BaseUrl.getUrl(BaseUrl.downFile) + "?filename=");
	}

	@SuppressLint("CheckResult")
	private static void load(final String id, final ImageView imageView, int type, final int defaultImg, String url) {
		if (TextUtils.isEmpty(id)) {
			LogUtil.e("BaseImageLoad", "图片id错误");
			if (defaultImg != 0) {
				imageView.setImageResource(defaultImg);
			}
			return;
		}

		RequestOptions requestOptions;
		if (type == Type.circular) {
			requestOptions = RequestOptions.circleCropTransform();
		} else if (type == Type.radius) {
			requestOptions = new RequestOptions();
		} else {
			requestOptions = new RequestOptions();
		}
		requestOptions.placeholder(defaultImg)
				.error(defaultImg)
				.diskCacheStrategy(DiskCacheStrategy.ALL);
//				.centerCrop();

		RequestManager requestManager = Glide.with(imageView);
		RequestBuilder<Drawable> requestBuilder;
		if (id.startsWith("http")) {
			url = id;
			requestBuilder = requestManager.load(url);
		} else {
			url = url + id;
			GlideUrl cookie = new GlideUrl(url, new LazyHeaders.Builder()
					.addHeader("Authorization", BaseTokenManager.getToken())
					.build());
			requestBuilder = requestManager.load(cookie);
		}


//		requestBuilder.transition(new DrawableTransitionOptions()
//				.crossFade())
//				.apply(requestOptions)
//				.into(imageView);
		requestBuilder
				.apply(requestOptions)
				.into(imageView);

	}

	@SuppressLint("CheckResult")
	public static Bitmap loadAndGet(final String id, final ImageView imageView) throws ExecutionException, InterruptedException {
		if (TextUtils.isEmpty(id)) {
			LogUtil.e("BaseImageLoad", "图片id错误");
			return null;
		}

		RequestOptions requestOptions = new RequestOptions();
		requestOptions.placeholder(defaultDrawableId)
				.error(defaultDrawableId)
				.diskCacheStrategy(DiskCacheStrategy.ALL);
//				.centerCrop();

		RequestManager requestManager = Glide.with(imageView);
		RequestBuilder<Bitmap> requestBuilder;
		String url = BaseUrl.getUrl(BaseUrl.downFile) + "?filename=";
		if (id.startsWith("http")) {
			url = id;
			requestBuilder = requestManager.asBitmap().load(url);
		} else {
			url = url + id;
			GlideUrl cookie = new GlideUrl(url, new LazyHeaders.Builder()
					.addHeader("Authorization", BaseTokenManager.getToken())
					.build());
			requestBuilder = requestManager.asBitmap().load(cookie);
		}


//		requestBuilder.transition(new DrawableTransitionOptions()
//				.crossFade())
//				.apply(requestOptions)
//				.into(imageView);
		return requestBuilder
				.apply(requestOptions)
				.submit().get();
	}

	private class GlideRoundTransform extends BitmapTransformation {
		@Override
		protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
			return null;
		}

		@Override
		public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

		}

//		private static float radius = 0f;
//
//		public GlideRoundTransform(Context context) {
//			this(context, 5);
//		}
//
//		public GlideRoundTransform(Context context, int dp) {
//			super(context);
//			this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
//		}
//
//		@Override
//		protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
//			Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
//			return roundCrop(pool, bitmap);
//		}
//
//		private  Bitmap roundCrop(BitmapPool pool, Bitmap source) {
//			if (source == null) return null;
//
//			Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
//			if (result == null) {
//				result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
//			}
//
//			Canvas canvas = new Canvas(result);
//			Paint paint = new Paint();
//			paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
//			paint.setAntiAlias(true);
//			RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
//			canvas.drawRoundRect(rectF, radius, radius, paint);
//			return result;
//		}
//
//		public String getId() {
//			return getClass().getName() + Math.round(radius);
//		}
//
//		@Override
//		public void updateDiskCacheKey(MessageDigest messageDigest) {
//
//		}

	}
}
